from sqlalchemy import Table, Column, Integer, ForeignKey, String, Float
from sqlalchemy.orm import relationship
from .database import Base

video_tag_association = Table(
    "video_tag_association",
    Base.metadata,
    Column("video_id", Integer, ForeignKey("videos.id")),
    Column("tag_id", Integer, ForeignKey("tags.id")),
)



class Tag(Base):
    __tablename__ = "tags"
    id = Column(Integer, primary_key=True, index=True)
    name = Column(String, index=True, unique=True)

    videos = relationship("Video", secondary=video_tag_association, back_populates="tags")



class Video(Base):
    __tablename__ = "videos"
    id = Column(Integer, primary_key=True, index=True)
    name = Column(String, index=True)
    url = Column(String, index=True)
    score = Column(Float)
    type = Column(String, index=True)
    thumbnail_url = Column(String, nullable=True)
    tags = relationship("Tag", secondary=video_tag_association, back_populates="videos")